<?php
// +----------------------------------------------------------------------
// | 生态墓销售流程
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\CemeteryModel;
use app\admin\model\ContractModel;
use app\admin\model\CustomerInfoModel;
use app\admin\model\OrderModel;
use app\admin\model\TombareaRowColModel;
use cmf\controller\AdminBaseController;
use think\Db;
use app\admin\model\MwProductLogModel;
use function Qiniu\entry;
use app\admin\model\AdminMenuModel;
use think\Exception;
use tree\Tree;

class ArchivesController extends AdminBaseController
{

    protected $selected_id_v = 0;
    protected $user_model;
    protected $dept_model;
    protected $order_model;
    protected $selected_id_a;
    protected $cemetery_model;



    public function initialize()
    {
        $this->user_model = Db::name("user");
        $this->dept_model = Db::name("department");
        $this->order_model = new OrderModel();
        $this->selected_id_a = session('selected_id_a');
        $this->cemetery_model = new CemeteryModel();
        parent::initialize();
    }
    //可视化墓区
    public function getSup($id)
    {
        // 拼接查询条件hql
        $res = $this->cemetery_model->where(array('is_delete' => 0, 'id' => $id))->select()->toArray();
        // 得到一级数据
        if ($res) {
            if ($res[0]["parent_id"] != 0) {
                $result = $this->getSup($res[0]["parent_id"]);
            } else {
                $result = array();
            }
            $imgInfo = array_merge_recursive($res, $result);
        } else {
            $imgInfo = array();
        }
        return $imgInfo;
    }

    public function visua()
    {
        $name = $this->request->param("name");
        //import("Tree");
        $menu = new Tree();
        $menu->icon = array('&nbsp;', '&nbsp;', '&nbsp;');
        $menu->nbsp = '';

        if(empty($name)){
            $result = $this->cemetery_model->cacheCemeteryData();
        }else{
            $result =  $this->cemetery_model->alias("c")
                ->field("c.id,c.name,c.parent_id as pId,c.type")
                ->join("dj_tombarea t","c.id = t.cemetery_id","left")
                ->join("dj_tombarea_row r","t.id = r.tombarea_id","left")
                ->join("dj_tombarea_row_col rc","r.rowID = rc.rowid","left")
                ->join("dj_archives_deceased d","d.tomb_id = rc.colID","left")
                ->where('c.is_delete',0)
                ->where('rc.num',$name)
                ->whereOr('d.deceased_name',"like","%".$name."%")
                ->order(array("c.list_order" => "ASC"))
                ->select()->toArray();
        }

        $tom = array();
        foreach($result as $k => $v){
            $tombarea = Db::name("tombarea")->field("id,name,'' as type")
                ->where(array("cemetery_id"=>$v["id"],"status"=>1))->order('list_order asc ')
                ->select()->toArray();
            foreach($tombarea as $i => $t){
                $tombarea[$i]['pId'] = $v["id"];
            }
            $tom = array_merge_recursive ( $tom, $tombarea);
        }
        $result = array_merge_recursive ( $tom, $result);

        $this->assign("categorys", json_encode($result));
        $this->assign("name", $name);
        $this->assign("selected_id_a", $this->selected_id_a?$this->selected_id_a:0);
        return $this->fetch();
    }

    /*
     * 根据陵园查询墓区
     */
    public function selectTombarea()
    {
        $id = $this->request->param("id");
        $result = Db::name("dj_tombarea")->field("id,t.align,t.row,p.selling_price")
            ->where(array('t.cemetery_id' => $id, 't.status' => 1))->select()->toArray();

        exit(json_encode($result));
    }

    /*
     * 根据陵园查询墓位信息
     */
    public function tombareacol()
    {
        $id = $this->request->param("id");
        $array_list = TombareaRowColModel::getTombRowColById($id);
        $result['data']=$array_list;
        exit(json_encode($result));
    }

    /*
     * 陵园墓区产品信息
     */
    public function product()
    {
        $tombar_id = $this->request->param('tombar_id');
        $this->assign("tombar_id", $tombar_id);
        $type = $this->request->param('type');
        $this->assign("type", $type);

        $tomb_id = $this->request->param('tomb_id');

        $info = Db::name("tombarea_row_col")->alias("c")
            ->join("dj_tombarea_information_product p", "c.colID = p.tomb_id", "left")
            ->whereIn('c.colID', $tomb_id)->find();


        $num = '';
        if ($info) {
            $result = Db::name('tombarea_information_product')->where(['tomb_id' => $info['colID'], 'cate_id' => $info['cate_id']])->find();
            if ($result) {
                if ($result['single_cavity_size']) {
                    $single_cavity_size = explode('*', $result['single_cavity_size']);
                    $result['sc_width'] = $single_cavity_size[0];
                    $result['sc_height'] = $single_cavity_size[1];
                    $result['sc_thick'] = $single_cavity_size[2];
                }

                $result['reacher'] = $info['reacher'];
            }
            $num = $info['num'];
            $cate_id = $info['cate_id'];
            if ($cate_id == 2) {
                // 详情
                if ($info['product_mb_base_id']) {
                    $base_id_arr = $info['product_mb_base_id'];
                    if ($info['product_mb_base_id1']) {
                        $base_id_arr = $info['product_mb_base_id'] . ',' . $info['product_mb_base_id1'];
                    }
                } else {
                    if ($info['product_mb_base_id1']) {
                        $base_id_arr = $info['product_mb_base_id1'];
                    }
                }
                $baseData = Db::name("product_mb_base")->whereIn('id', $base_id_arr)->select();
                $this->assign('baseData', $baseData);
            }
        }

//        // 石材种类
        $cate_type = Db::name("stuff")->alias('s')->field("s.id,concat(c.cate_name,'-',s.stuff_name) as name")->join("dj_cate c", "c.id = s.cate_id", "left")->select();
        $this->assign("cate_type", json_encode($cate_type));
//
//        // 附件
        $annex = Db::name("annex")->field("id,annex_name as name")->select();
        $this->assign("annexJson", json_encode($annex));
//
        // 陵园id
        $cemeData = Db::name("tombarea")->alias('s')->field("c.id,c.parent_id")->join("dj_cemetery c", "c.id = s.cemetery_id", "left")->where(array('s.id' => $tombar_id))->find();
        $this->assign("ceme_id", $cemeData['id']);
        $this->assign("parent_id", $cemeData['parent_id']);


        $this->assign('info', $info);
        $this->assign('cate_id', $cate_id);
//        $this->assign('cate_type',$info['cate_id']);
        $this->assign('result', $result);
        $this->assign('num_sn', $num);
        $this->assign('col_id', $tomb_id);
        $mblist = Db::name('product')->where('pro_type', $cate_id)->select();
        $this->assign('mb_list', $mblist);


        return $this->fetch();
    }

    public function productysmbView()
    {
        // 石材种类
        $this->getALlOption();


        $id = $this->request->param("id");
        $type = $this->request->param('type');
        $where = array(
            'id' => $id
        );
        $result = Db::name("product_mb")->where($where)->find();
        $this->assign($result);
        // 详情
        $infowhere = array(
            'pro_id' => $id,
            'itype' => 1
        );
        $infoData = Db::name("product_mb_info")->where($infowhere)->select();
        $this->assign('infoData', $infoData);
        // 基础
        $basewhere = array(
            'pro_id' => $id,
            'itype' => 2
        );
        $baseData = Db::name("product_mb_info")->where($basewhere)->select();
        $this->assign('baseData', $baseData);
        // 盖板
        $platewhere = array(
            'pro_id' => $id,
            'itype' => 3
        );
        $plateData = Db::name("product_mb_info")->where($platewhere)->select();
        $this->assign('plateData', $plateData);

        // 栏杆
        $railwhere = array(
            'pro_id' => $id,
            'itype' => 4
        );
        $railData = Db::name("product_mb_info")->where($railwhere)->select();
        $this->assign('railData', $railData);

        // 附件
        $annexwhere = array(
            'pro_id' => $id,
        );
        $annexData = Db::name("product_mb_annex")->where($annexwhere)->select();
        $this->assign('annexData', $annexData);
        // echo Db::name("product_mb_annex")->getLastSql();
        $this->assign('type_mb', $type);
        return $this->fetch();
    }

    public function getProductInfo()
    {
        $id = $this->request->param("id");
        $data = Db::name("product")->alias('s')->field("s.*,p.pro_name as name")->join("dj_product p", "p.id = s.change_id", "left")->where(array("s.id" => $id))->find();
        // 详情
        $infowhere = array(
            'pro_id' => $id,
            'itype' => 1
        );
        $infoData = Db::name("product_info")->where($infowhere)->select();
        // 基础
        $basewhere = array(
            'pro_id' => $id,
            'itype' => 2
        );
        $baseData = Db::name("product_info")->where($basewhere)->select();
        // 盖板
        $platewhere = array(
            'pro_id' => $id,
            'itype' => 3
        );
        $plateData = Db::name("product_info")->where($platewhere)->select();
        // 栏杆
        $railwhere = array(
            'pro_id' => $id,
            'itype' => 4
        );
        $railData = Db::name("product_info")->where($railwhere)->select();
        // 附件
        $annexwhere = array(
            'pro_id' => $id,
        );
        $annexData = Db::name("product_annex")->where($annexwhere)->select();


        $result = array(
            'infoData' => $infoData,
            'baseData' => $baseData,
            'plateData' => $plateData,
            'railData' => $railData,
            'annexData' => $annexData,
            'data' => $data
        );

        exit(json_encode($result));
    }

    public function productadd_post()
    {
        // 墓区id
        $tombar_id = $this->request->param('tombar_id');
        $data = $this->request->param();
        if ($data['id'] != 0) {
            Db::name("tombarea_product")->where('id', $data['id'])->delete();
        }

        $productData['tombar_id'] = $tombar_id;
        $productData['pro_status'] = $data['pro_status'];
        $productData['footprint'] = $data['footprint'];
        $productData['tombstone_h'] = $data['tombstone_h'];
        $productData['tomb_regulation'] = $data['tomb_regulation'];
        $productData['warranty'] = $data['warranty'];
        $productData['specifications'] = $data['specifications'];
        $productData['green_belt'] = $data['green_belt'];
        $productData['selling_price'] = $data['selling_price'];
        $productData['manage_cycle'] = $data['manage_cycle'];
        $productData['supplier'] = $data['supplier'];
        $productData['old_pro'] = $data['old_pro'];
        $productData['new_pro'] = $data['new_pro'];
//      $productData['pro_photo'] = $data['pro_photo'];
        $productData['add_user'] = cmf_get_current_admin_id();
        $productData['add_time'] = time();

        Db::name("tombarea_product")->insert($productData);
        // 陵园id
        $cemeData = Db::name("tombarea")->alias('s')->field("c.id,c.parent_id")->join("dj_cemetery c", "c.id = s.cemetery_id", "left")->where(array('s.id' => $tombar_id))->find();
        $id = $cemeData['id'];
        $parent_id = $cemeData['parent_id'];

        $this->success("保存成功", url("visua/index"));
    }

    /**
     * 获取菜单深度
     * @param $id
     * @param $array
     * @param $i
     */
    protected function _get_level($id, $array = array(), $i = 0)
    {

        if ($array[$id]['parent_id'] == 0 || empty($array[$array[$id]['parent_id']]) || $array[$id]['parent_id'] == $id) {
            return $i;
        } else {
            $i++;
            return $this->_get_level($array[$id]['parent_id'], $array, $i);
        }

    }

    /**
     *  编辑墓区 信息
     */
    public function Information()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $col_id = explode(',', $param['col_id']);
            $id = explode(',', $param['id']);

            foreach ($col_id as $key => &$value) {
//               $tomb_address = Db::name('tombarea_row_col')->where('colID',$value)->find();
                $tomb_address = Db::name('tombarea_row_col')->alias('c')
                    ->join('dj_tombarea_row r', '  c.rowid = r.rowID')
                    ->join('dj_tombarea t', 'r.tombarea_id = t.id')
                    ->field('c.colID,c.num,c.rowid,r.tombarea_id,t.cate_id,c.reacher,t.cemetery_id,t.name tomb_address,c.rows_num tomb_loor,c.column_num tomb_bit_number ')
                    ->where('c.colID', $value)->find();
                $tomb_region = $this->getSup($tomb_address['cemetery_id']);
                $region = '';
                $region_ceng = '';
                if (isset($param['layer_num'])) {
                    $region_ceng = $param['layer_num'];
                } else {
                    $region_ceng = $tomb_address['tomb_loor'];
                }

                foreach ($tomb_region as $k => &$v) {
                    if ($v['type'] == '层') {
                        $region_ceng = $v['name'];
                    } else {
                        $region = $v['name'] . $region;
                    }
                }
                $data = [
                    'tomb_id' => $value,
                    'tomb_num' => $tomb_address['num'],
                    'tombar_id' => $tomb_address['tombarea_id'],
                    'cemetery_id' => $tomb_address['cemetery_id'],
//                   'tomb_sn'=>$tomb_sn[$key],
                    'cate_id' => $param['cate_id'], //类型
                    'pro_status' => $param['pro_status'], //类型
                    'funeral' => isset($param['funeral']) ? $param['funeral'] : '',//墓穴规制
                    'tomb_regulation' => isset($param['tomb_regulation']) ? $param['tomb_regulation'] : '',
                    'footprint' => isset($param['footprint']) ? $param['footprint'] : '',//墓碑高度/空间高度/左右长度
                    'tombstone_w' => isset($param['tombstone_w']) ? $param['tombstone_w'] : '',//占地宽度/空间宽度/纵深宽度
                    'tombstone_h' => isset($param['tombstone_h']) ? $param['tombstone_h'] : '',//墓碑高度/空间高度/左右长度
                    'single_cavity_size' => !empty($param['sc_width']) ? $param['sc_width'] . '*' . $param['sc_height'] . '*' . $param['sc_thick'] : '',//单穴最大
                    'convergence' => isset($param['convergence']) ? $param['convergence'] : '',//收敛形式
                    'specifications' => isset($param['green_belt']) ? $param['specifications'] : '',//拜道规格
                    'green_belt' => isset($param['green_belt']) ? $param['green_belt'] : '', // 绿化带
                    'greenbelt_num' => isset($param['greenbelt_num']) ? $param['greenbelt_num'] : '',
                    'maintain' => isset($param['maintain']) ? $param['maintain'] : '',//墓穴维护期  ,
                    'warranty' => isset($param['warranty']) ? $param['warranty'] : '',//质保期
                    'is_bye' => isset($param['is_bye']) ? $param['is_bye'] : 0,//1含拜道0不
                    'limit_height' => isset($param['limit_height']) ? $param['limit_height'] : '',//限制高度
                    'design' => isset($param['design']) ? $param['design'] : '',//设计墓穴
                    'periphery' => isset($param['periphery']) ? $param['periphery'] : '',//外围设置
                    'current_make' => isset($param['current_make']) ? $param['current_make'] : '',//当前制作
                    'new_pro' => isset($param['new_pro']) ? $param['new_pro'] : '',//新产品
//                   'new_pro'=>isset($param['old_pro'])?$param['old_pro']:isset($param['new_pro'])?$param['new_pro']:'',//以旧换新产品
                    'old_pro' => isset($param['old_pro']) ? $param['old_pro'] : '',//以旧换新产品
                    'product_mb_base_id' => isset($param['product_mb_base_id']) ? $param['product_mb_base_id'] : '',
                    'product_mb_base_id1' => isset($param['product_mb_base_id']) ? $param['product_mb_base_id1'] : '',
                    'product_info_id' => isset($param['product_info_id']) ? $param['product_info_id'] : '',
                    'selling_price' => isset($param['selling_price']) ? $param['selling_price'] : '',//售价
                    'menu_type' => isset($param['menu_type']) ? $param['menu_type'] : '',//售价单位
                    'tomb_region' => $region,//墓位所在区域
                    'tomb_address' => $tomb_address['tomb_address'],//所在位置
                    'layer_num' => $region_ceng,//所在层
                    'tomb_bit_number' => $tomb_address['tomb_bit_number'] . '号位',//所在位号
                    'add_user' => cmf_get_current_admin_id(),
                ];
                if ($param['cate_id'] == 2) {
                    $data['tom_img'] = $param['upload_plan'];
                } else {
                    $data['tom_img'] = $param['tom_img'];
                }
                if (isset($param['old_pro']) ? $param['old_pro'] : '') {
                    $data['new_pro'] = $param['old_pro'];

                    $data['tom_img'] = $param['upload_plan'];

                    MwProductLogModel::add($value, $data['new_pro'], $param['old_pro']);
                }
                if ($data['new_pro'] || $param['cate_id'] == 2) {
                    $ingo_col = Db::name('tombarea_row_col')->where(['colID' => $value])->find();
                    if ($ingo_col['col_status'] == 0) {
                        Db::name('tombarea_row_col')->where(['colID' => $value])->update(['col_status' => 1]);
                    }
                }
                if ($param['substitute']) {
                    Db::name('tombarea_row_col')->where(['colID' => $value])->update(['reacher' => $param['substitute']]);
                } else {
                    Db::name('tombarea_row_col')->where(['colID' => $value])->update(['reacher' => '']);
                }

//               exit(json_encode($param['mb'] ));

                Db::name("tomb_mb")->where(['tomb_id' => $value, 'type' => 0])->delete();
                if (isset($param['mb'])) {
                    foreach ($param['mb'] as $km => &$valuem) {
                        if (!empty($valuem['dele_info_mb_id'])) {
//                           Db::name('product_mb_info')->where(['id' => $valuem['dele_info_mb_id']])->delete();
                            Db::name("tomb_mb")->where(['tomb_id' => $value, 'id' => $valuem['dele_info_mb_id'], 'type' => 0])->delete();

                        } else {
                            if (!empty($valuem['mb_id'])) {
                                $mb_info = [
                                    'tomb_id' => $value,
                                    'mb_id' => $valuem['mb_id'],
                                    'mb_name' => $valuem['mb_name'],
                                    'address' => $valuem['address'],
                                    'status' => 0,
                                    'type' => 0,
                                ];
                                if (!empty($valuem['mb_id_id'])) {
                                    Db::name("tomb_mb")->insertGetId($mb_info);
                                } else {
                                    Db::name("tomb_mb")->insertGetId($mb_info);
                                }
                            }
                        }
                    }
                }
                if (isset($param['other'])) {
                    foreach ($param['other'] as $ko => &$valueother) {
                        if (!empty($valueother['dele_product_info_id'])) {
//                           Db::name('product_mb_info')->where(['id' => $valueother['dele_product_info_id']])->delete();
                            Db::name("tomb_mb")->where(['tomb_id' => $value, 'info_id' => $valueother['dele_product_info_id'], 'type' => 7])->delete();
                        } else {

                            $product_info = array(
                                'itype' => 7,
                                'mb_id' => $value,
                                'iname' => $valueother['iname'],
                                'iwidth' => $valueother['iwidth'],
                                'iheight' => $valueother['iheight'],
                                'ithick' => $valueother['ithick'],
                                'icate' => $valueother['icate'],
                                'inumber' => $valueother['inumber'],
                                'imaterial_price' => $valueother['imaterial_price'],
                                'irepair_price' => $valueother['irepair_price'],
                                'ichange_price' => $valueother['ichange_price'],
                                'isort' => $valueother['isort'],
                                'add_time' => time(),
                                'add_user' => cmf_get_current_admin_id(),
                            );
                            if (!empty($valueother['product_info_id'])) {
                                $res = Db::name('product_mb_info')->where('id', $valueother['product_info_id'])->update($product_info);
                                $mb_info_id = $valueother['product_info_id'];
                                $product_info_arr[] = $product_info;
                            } else {
                                $mb_info_id = Db::name('product_mb_info')->insertGetId($product_info);


                            }
                            if (!Db::name("tomb_mb")->where(['tomb_id' => $value, 'mb_info_id' => $mb_info_id, 'type' => 7])->find()) {
                                $mb_infos = [
                                    'tomb_id' => $value,
                                    'mb_id' => $valuem['mb_id'],
                                    'mb_name' => '',
                                    'mb_info_id' => $mb_info_id,
                                    'address' => $valuem['address'],
                                    'status' => 0,
                                    'type' => 7,
                                ];
                                Db::name("tomb_mb")->insertGetId($mb_infos);
                            }
                        }
                    }
                }
                $information_info = Db::name("tombarea_information_product")->where(['tomb_id' => $value])->find();
                if ($information_info) {
//                   exit(json_encode($information_info['id']));
                    $res = Db::name("tombarea_information_product")->where(['id' => $information_info['id']])->update($data);
                } else {
                    $data['add_time'] = time();
                    $res = Db::name("tombarea_information_product")->insertGetId($data);
                }
            }

            if ($res !== false) {
                $this->success("保存成功", url("visua/index"));
            } else {
                $this->error('操作失败');
            }
        }
        $id = $this->request->param('col_id');
        $info = Db::name('tombarea_row_col')->alias('c')
            ->join('dj_tombarea_row r', '  c.rowid = r.rowID')
            ->join('dj_tombarea t', 'r.tombarea_id = t.id')
            ->field('c.colID,c.num,c.rowid,r.tombarea_id,t.cate_id,c.reacher,t.cemetery_id,t.name tomb_address,c.rows_num tomb_loor,c.column_num tomb_bit_number ')
            ->whereIn('c.colID', $id)->find();

        $num = '';
//	    foreach ($info as $key=>&$value){
//	        $num  = !empty($num)?$num.'，'.$value['num']:$value['num'];
//        }
        if ($info) {
            $colID = Db::name('tombarea_row_col')->whereIn('colID', $id)->column('colID');
            $num = Db::name('tombarea_row_col')->whereIn('colID', $id)->column('num');
            $num = implode(',', $num);
            $result = Db::name('tombarea_information_product')->whereIn('tomb_id', $colID)->where(['cate_id' => $info['cate_id']])->find();
            $result_id = Db::name('tombarea_information_product')->whereIn('tomb_id', $colID)->where(['cate_id' => $info['cate_id']])->column('id');

            if ($result) {
                if ($result['single_cavity_size']) {
                    $single_cavity_size = explode('*', $result['single_cavity_size']);
                    $result['sc_width'] = $single_cavity_size[0];
                    $result['sc_height'] = $single_cavity_size[1];
                    $result['sc_thick'] = $single_cavity_size[2];
                }
            }
            $result['substitute'] = $info['reacher'];
            $cate_id = $info['cate_id'];

            if ($cate_id == 2) {
                $tomb_mb_list = Db::name("tomb_mb")->where(['tomb_id' => $colID, 'type' => 0])->select();
                $result['infoData'] = $tomb_mb_list;
            }
            $cate_type = Db::name("stuff")->alias('s')->field("s.id,concat(c.cate_name,'-',s.stuff_name) as name")->join("dj_cate c", "c.id = s.cate_id", "left")->select();

            $mb_info_id = Db::name("tomb_mb")->where(['tomb_id' => $colID, 'type' => 7])->column('mb_info_id');
            $product_info = Db::name('product_mb_info')->whereIn('id', $mb_info_id)->select();
        }

        $this->assign("product_info", $product_info);
        $this->assign("cate_type", $cate_type);
        $this->assign("cateJson", json_encode($cate_type));


        $this->assign('info', $info);
        $this->assign('result', $result);
        $this->assign('num_sn', $num);
        $this->assign('col_id', $id);
        $mblist = Db::name('product')->where('pro_type', $cate_id)->select();

        $mb_arr = [];
        foreach ($mblist as $km => &$vals) {
            $m_info['id'] = $vals['id'];
            $m_info['pro_name'] = $vals['pro_name'] . '(' . $vals['pro_code'] . ')【' . $this->regulation_arr[$vals['regulation']] . '】';
            $m_info['pro_code'] = $vals['pro_code'];
            $m_info['pro_photo'] = $vals['pro_photo'];
            $m_info['pro_photo_path'] = cmf_get_image_preview_url($vals['pro_photo']);
            $mb_arr[] = $m_info;
        }

        $this->this_tomb();
        $this->assign('mb_list', $mb_arr);
        $this->assign('cate_id', $cate_id);
        return $this->fetch();

    }

    public function getProductMbInfo()
    {
        $result = [];
        $parem = $this->request->param();
        $id = $parem['id'];
        if ($parem['type'] == 1) {
            $result = [];
        } elseif ($parem['type'] == 2) {
            $result['product_mb_base'] = Db::name('product_mb_base')->where('product_id', $id)->select();
            $result['product_info'] = Db::name('product_info')->where(['pro_id' => $id])->select();
        } elseif ($parem['type'] == 3) {
        } elseif ($parem['type'] == 4) {

            $result = [];
        }
        return $this->result($result);
    }

    /**
     * 删除墓位
     */
    public function tombareacoldelete()
    {
        if ($this->request->isGet()) {
            $param = $this->request->param();
            $status = true;
            $res = true;
            $msg = '删除成功';
            $msg_[] = '删除成功';
            Db::startTrans();
            $col_id = explode(',', $param['id']);
            try {
                foreach ($col_id as $k => &$value) {
                    $res_info = Db::name('tombarea_row_col')->where(['colID' => $value])->find();
                    if ($res_info) {
                        if ($res_info['col_status'] == 0) {
                            $msg_[$k + 1] = 1;
                            $res1 = Db::name('tombarea_row_col')->where(['colID' => $value])->update(['status' => 0]);
//                            $res_info = Db::name('tombarea_row')->where(['rowID'=>$res_info['rowid']])->dec('column',1);
                            if (!$res1) {
                                $msg_[$k + 2] = 2;
                                $status = false;
                            }
                            Db::commit();
                        } else {
                            $msg_[$k + 3] = 3;
                            Db::rollback();
                            $status = false;
                            $msg = '删除失败，有关联相关信息！';
                        }
                    } else {
                        $msg_[$k + 4] = 4;
                        Db::rollback();
                        $msg = '删除失败';
                    }
                }
                if ($res && $status) {
                    Db::commit();
                    $this->success($msg, url('index'));
                } else {
                    Db::rollback();
                    $this->error($msg, url('index'));
                }

            } catch (Exception $e) {
                Db::rollback();
                $this->error($msg, url('index'));
                exit(json_encode(13213));
            }

        }
    }

    /**
     *  保留
     */
    public function tombareacolretain()
    {
        $param = $this->request->param();
        $status = true;
        $col_id = explode(',', $param['id']);
        foreach ($col_id as $k => &$value) {
            $res_info = Db::name('tombarea_row_col')->where(['colID' => $value])->find();
            if ($res_info) {
                if ($res_info['col_status'] != 6 || $res_info['col_status'] != 7) {
                    $msg_[$k + 1] = 1;
                    $res1 = Db::name('tombarea_row_col')->where(['colID' => $value])->update(['col_status' => 2]);
                    if (!$res1) {
                        $status = false;
                    }
                }
            }
        }
        if ($status) {
            $this->success('保留成功', url('index'));
        } else {
            $this->error('保留失败');
        }
    }

    /**
     * 取消保留
     */
    public function tombCancelReservation()
    {
        $param = $this->request->param();
        $status = true;
        $col_id = explode(',', $param['id']);
        foreach ($col_id as $k => &$value) {
            $info = Db::name('tombarea_row_col')->where(['colID' => $value])->find();
            $product_info = Db::name('tombarea_product')->where(['tombar_id' => $info['rowid']])->find();
            if ($info) {
                if ($info['col_status'] != 6 || $info['col_status'] != 7) {
                    if ($product_info) {
                        Db::name('tombarea_row_col')->where(['colID' => $value])->update(['col_status' => 1]);
                    } else {
                        Db::name('tombarea_row_col')->where(['colID' => $value])->update(['col_status' => 0]);
                    }
                } else {
                    $status = false;
                }
            }
        }
        if ($status) {
            $this->success('取消保留成功', url('index'));
        } else {
            $this->error('取消保留失败！');
        }
    }

    /**
     * 隐藏
     */
    public function tombCancelhide()
    {
        $param = $this->request->param();
        $status = true;
        $col_id = explode(',', $param['id']);
        foreach ($col_id as $k => &$value) {
            $info = Db::name('tombarea_row_col')->where(['colID' => $value])->find();
            if ($info) {
                Db::name('tombarea_row_col')->where(['colID' => $value])->update(['status' => 2]);

            }
        }
        if ($status) {
            $this->success('隐藏成功', url('index'));
        } else {
            $this->error('隐藏失败！');
        }
    }

    /**
     * 取消隐藏
     */
    public function tombCancelUnhide()
    {
        $param = $this->request->param();
        $status = true;
        $col_id = explode(',', $param['id']);
        foreach ($col_id as $k => &$value) {
            $info = Db::name('tombarea_row_col')->where(['colID' => $value])->find();
            $product_info = Db::name('tombarea_product')->where(['tombar_id' => $info['rowid']])->find();
            if ($info) {
                Db::name('tombarea_row_col')->where(['colID' => $value])->update([['status' => 1]]);
            }
        }
        if ($status) {
            $this->success('取消隐藏成功', url('index'));
        } else {
            $this->error('取消隐藏失败！');
        }
    }

    /**
     * 合并
     */
    public function tombMerge()
    {
        $param = $this->request->param();
        $status = true;
        $col_id = explode(',', $param['id']);
        if ($status) {
            $this->success('取消合并成功', url('index'));
        } else {
            $this->error('取消合并失败！');
        }
    }

    /**
     * 取消合并
     */
    public function tombCancelMerge()
    {
        $param = $this->request->param();
        $status = true;
        $col_id = explode(',', $param['id']);
        if ($status) {
            $this->success('取消合并成功', url('index'));
        } else {
            $this->error('取消合并失败！');
        }
    }

    /**
     * 下拉数据
     */
    public function getALlOption()
    {
        $cate_type = Db::name("stuff")->alias('s')->field("s.id,concat(c.cate_name,'-',s.stuff_name) as name")->join("dj_cate c", "c.id = s.cate_id", "left")->select();
        $this->assign("cate_type", $cate_type);
        $this->assign("cateJson", json_encode($cate_type));
        // 所有产品库
        $allData = Db::name("product")->field("id,pro_name as name")->select();
        $this->assign("allData", $allData);
        // 附件
        $annex = Db::name("annex")->field("id,annex_name as name")->select();
        $this->assign("annex", $annex);
        $this->assign("annexJson", json_encode($annex));
    }

    public function this_tomb()
    {

        $tomb_regulation = [
            ['id' => 1, 'name' => '单穴'],
            ['id' => 2, 'name' => '双穴'],
            ['id' => 3, 'name' => '三穴'],
        ];

        $tomb_funeral_1 = [
            ['id' => '水葬', 'name' => '水葬'],
            ['id' => '树葬', 'name' => '树葬'],
            ['id' => '草坪葬', 'name' => '草坪葬'],
        ];
        $tomb_convergence_1 = [
            ['id' => '骨灰盒', 'name' => '骨灰盒'],
            ['id' => '骨灰坛', 'name' => '骨灰坛'],
            ['id' => '棺椁', 'name' => '棺椁'],
            ['id' => '其他', 'name' => '其他'],
        ];
        $tomb_convergence_4 = [
            ['id' => '骨灰盒', 'name' => '骨灰盒'],
            ['id' => '骨灰坛', 'name' => '骨灰坛'],
            ['id' => '棺椁', 'name' => '棺椁'],
            ['id' => '其他', 'name' => '其他'],
        ];

        $this->assign('tomb_regulation', $tomb_regulation);
        $this->assign('tomb_funeral_1', $tomb_funeral_1);
        $this->assign('tomb_convergence_1', $tomb_convergence_1);
        $this->assign('tomb_convergence_4', $tomb_convergence_4);
    }


    /**
     * 档案管理
     * @return mixed
     */
    public function archives()
    {
        $archives_sn = $this->request->param("archives_num");
        $tomb_num = $this->request->param("tomb_num");
        $customer_name = $this->request->param("customer_name");
        $start_time = $this->request->param("start_time");
        $end_time = $this->request->param("end_time");
        if (empty($end_time)) {
            $end_time = 99999999999;
        }else{
            $end_time = strtotime($end_time . '+1 day');
        }
        if (empty($start_time)) {
            $start_time = -99999999999;
        }else{
            $start_time = strtotime($start_time);
        }
        $n1 = 'DESC';
        $n2 = 'ASC';

        $results = $this->order_model->alias('f')->field('f.*,fb.buyer_name,fb.buyer_phone,p.tomb_region,p.tomb_address,fb.id,f.tomb_id')
            ->join('dj_customer_info fb', 'fb.id=f.customer_id')
            ->join('dj_tombarea_information_product p', 'p.tomb_id=f.tomb_id')
            ->where('f.archives_sn', 'like', '%' . $archives_sn . '%')
            ->where('fb.buyer_name', 'like', '%' . $customer_name . '%')
            ->where('f.tomb_num', 'like', '%' . $tomb_num . '%')
//            ->where('customer_name', 'like', '%' . $customer_name . '%')
            ->whereTime('f.closing_time', '>=', $start_time)
            ->whereTime('f.closing_time', '<=', $end_time)
            ->order('f.tomb_num', $n2)
            //->order('file_num',$sort)
            //->order('closing_time',$sort)
            ->paginate(15);

        $page = $results->render();
        $this->assign("results", $results);
        $this->assign("page", $page);
        return $this->fetch();
    }

//    /*档案添加--跳转*/
//    public function add()
//    {
//        $results = Db::name("user")->alias('u')->field('u.*,d.department_name')
//            ->join('dj_department d', 'd.id=u.dept_id')
//            ->order('d.department_name', 'DESC')
//            ->select();
//
//        $this->assign("users", $results);
//        $this->assign('user_nickname', cmf_get_admin_name());
//        return $this->fetch();
//    }
//
//    /*档案添加--保存*/
//    public function add_post()
//    {
//        $data = $this->request->param();
//
//        //前台获取要插入的数据,order表
//        $new_data = array(
////            档案号
//            'archives_sn' => $data['archives_sn'],
////            墓位编号
//            'tomb_num' => $data['tomb_num'],
////            成交时间
//            'closing_time' => $data['closing_time'],
////            销售价格
//            'selling_price' => $data['selling_price'],
////            折扣让利
//            'discount' => $data['discount'],
////            墓区类型
//            'tomb_type' => $data['tomb_type'],
////            成交价格
//            'closing_price' => $data['closing_price'],
////            导购员
//            'shoppingguide' => $data['shoppingguide'],
////            代理商
//            'agent_user' => $data['agent_user'],
////            经办人
//            'handle' => cmf_get_current_admin_id(),
//        );
//
//        //tombarea_information_product表
//        $new_datas = array(
////            所在区域
//            'tomb_region' => $data['tomb_region'],
////            具体位置
//            'tomb_address' => $data['tomb_address'],
//        );
//
//        $closing_time = $data['closing_time'];
//
//        $new_data['closing_time'] = strtotime($closing_time);
//
//        $id = Db::name('order')->insertGetId($new_data);
//        Db::name('tombarea_information_product')->insertGetId($new_datas);
//
//        $this->file_buyer_add();
//        if ($id != false) {
//            $this->file_deceased_add($id);
//            $this->success("添加成功", url('Archives/index'));
//        } else {
//            $this->error("添加失败");
//        }
//    }

    /*购买人信息添加*/
    public function file_buyer_add($dz_time,$tomb_num)
    {
        $album = array(
            'add_time' => time(),
            'add_user' => cmf_get_current_admin_id(),
//            客户名称
            'buyer_name' => $_POST['buyer_name'],
//            联系电话
            'buyer_phone' => $_POST['buyer_phone'],
//            客户账户
            'customer_account' => $_POST['customer_account'],
//            身份证号
            'cart_ID' => $_POST['cart_ID'],
//            证件类型
            'cart_type' => $_POST['cart_type'],
//            联系地址
            'customer_address' => $_POST['customer_address'],
//            身份证地址
            'cart_upload' => $_POST['cart_upload'],
//            性别
            'buyer_sex' => $this->getCardSex($_POST['cart_ID'])
        );
        if($dz_time){
            $album['mz_dy_time'] = strtotime($dz_time);
            $album['is_dy_mz'] = 1;
            $album['tomb_number'] = $tomb_num;
        }
        $id = Db::name('customer_info')->insertGetId($album);
        return $id;
    }
    public function getCardSex($idcard) {
        if(empty($idcard)) return 1;
        $sexint = (int) substr($idcard, 16, 1);
        return $sexint % 2 === 0 ? '2' : '1';
    }
    /*逝者信息添加*/
    public function file_deceased_add($order_id)
    {
        $deceased_name = $_POST['deceased_name'];
        $gender = $_POST['gender'];

        $date_birth = $_POST['date_birth'];
        $date_death = $_POST['date_death'];
        $date_funeral = $_POST['date_funeral'];
        $funeral_time = $_POST['funeral_time'];

        $convergence_type = $_POST['convergence_type'];
        $type = $_POST['type'];
        $receptionist = $_POST['receptionist'];
        $relationship_users = $_POST['relationship_users'];

        for ($i = 0; $i < count($deceased_name); $i++) {
            $album[$i]['order_id'] = $order_id;
            $album[$i]['add_time'] = time();
            $album[$i]['add_user'] = cmf_get_current_admin_id();
            $album[$i]['deceased_name'] = $deceased_name[$i];
            $album[$i]['gender'] = $gender[$i];

            //时间类型，先获取值再转
            $album[$i]['date_birth'] = $date_birth[$i];
            $album[$i]['date_death'] = $date_death[$i];
            $album[$i]['date_funeral'] = $date_funeral[$i];
            $album[$i]['funeral_time'] = $funeral_time[$i];

            $album[$i]['convergence_type'] = $convergence_type[$i];
            $album[$i]['gender'] = $gender[$i];
            $album[$i]['type'] = $type[$i];
            $album[$i]['receptionist'] = $receptionist[$i];
            $album[$i]['relationship_users'] = $relationship_users[$i];
        }
        Db::name("Archives_deceased")->insertAll($album);
    }

    /*档案编辑跳转*/
    public function edit()
    {
        $tomb_id = $this->request->param('tomb_id');
        $order_id = $this->request->param('order_id');
        $selected_id_a = $this->request->param('selected_id_a');
        session('selected_id_a', isset($selected_id_a)?$selected_id_a:'0');
        if(!empty($tomb_id)) {
            if(!empty($order_id)){
                $result = Db::name("order")->where("tomb_id",$tomb_id)->where("order_id",$order_id)->find();
            }else{
                $result = Db::name("order")->where("tomb_id",$tomb_id)->find();
            }
            if(empty($result)){
                $result['order_id']='';
                $result['closing_time']='';
                $result['dz_time']='';
                $result['closing_price']='';
                $result['shoppingguide']='';
                $result['agent_user']='';
                $result['discount']='';
                $result['whq_start_date']='';
                $result['archives_sn']=date('Ymdhis');
            }
            if(!empty($result['whq_start_date'])){
                $result['whq_start_date'] = date('Y-m-d',$result['whq_start_date']);
            }
            if(!empty($result['closing_time'])){
                $result['closing_time'] = date('Y-m-d',$result['closing_time']);
            }
            if(!empty($result['dz_time'])){
                $result['dz_time'] = date('Y-m-d',$result['dz_time']);
            }
            $this->assign($result);

            // 查询合同表 根据Oder_id
            $con_is = Db::name('contract')->alias('c')->field('c.*,f.filename contract_enclosure_file_name')
                ->leftJoin('dj_asset f','c.contract_enclosure_file_id  = f.id')
                ->where(['order_id'=>$order_id])->find();
            if($con_is['sign_time'] == 0 || empty($con_is['sign_time'])){
                $con_is['sign_time'] = '';
                $con_is['contract_enclosure'] ='';
                $con_is['contract_id'] ='';
                $con_is['contract_sn'] ='';
            }else{
                $con_is['sign_time'] = date('Y-m-d',$con_is['sign_time']);
            }

            $this->assign("con_is",$con_is);
        }

        $this->info_product($tomb_id);

        $col_info = TombareaRowColModel::where(['colID'=>$tomb_id])->find();

        $this->assign('col_info', $col_info);
        $this->file_buyer($order_id);
        $this->file_deceased($order_id);
        //代理商
        $daili = dept_per(8);
        $this->assign('daili', $daili);
        //业务部
        $yewu = dept_per(1);
        $this->assign('yewu', $yewu);
        //接待人
        $jiedai = dept_per([1,4]);
        $this->assign('deptPer', $jiedai);

        $this->assign('user_name', cmf_get_admin_name());

        return $this->fetch();
    }

    /*获取墓位信息及产品信息表*/
    public function info_product($tomb_id)
    {
        $result = Db::name("tombarea_information_product")->where("tomb_id",$tomb_id)
            ->find();
        if(empty($result)){
            $this->error("请先添加墓位信息！");
        }
        $this->assign($result);
    }

    /*编辑-获取购买人表*/
    public function file_buyer($order_id)
    {
        $result = Db::name("customer_info")->alias('c')
            ->field("c.*")
            ->join('dj_order o', 'o.customer_id=c.id')
            ->where('o.order_id', $order_id)
            ->find();
        $this->assign('buyer',$result);
    }

    /*编辑-获取逝者信息表*/
    public function file_deceased($order_id)
    {
        $convergence_type = $this->request->param('convergence_type');

        $result = Db::name("Archives_deceased")->alias('fd')
            ->field('fd.deceased_id,fd.deceased_name,fd.gender,fd.date_birth,fd.date_death,fd.cremation_certificate,fd.cremation_certificate_file_id,
                fd.date_funeral,fd.funeral_time,fd.convergence_type,fd.type,fd.receptionist,fd.relationship_users')
            ->where('fd.order_id', $order_id)
            ->select();
        if($result){

            if(!empty($result['date_birth'])){
                $result['date_birth'] = date('Y-m-d',$result['date_birth']);
            }
            if(!empty($result['date_death'])){
                $result['date_death'] = date('Y-m-d',$result['date_death']);
            }
            if(!empty($result['funeral_time'])){
                $result['funeral_time'] = date('Y-m-d H:i',$result['funeral_time']);
            }
        }
        $this->assign('deceased', $result);
    }

    /*档案编辑保存*/
    public function edit_post()
    {
        $id = $this->request->param('tomb_id');
        $order_id = $this->request->param('order_id');
        $data = $this->request->param();
        $contract_enclosure = $this->request->param("contract_enclosure");
        $contract_enclosure_file_id = $this->request->param("contract_enclosure_file_id");
        $sign_time = $this->request->param("sign_time");
        $contract_id = $this->request->param("contract_id");
//dump($data);exit;
        $col_status = $data['col_status'];
        $dz_time = "";
        if($data['dz_time']){
            $dz_time = $data['dz_time'];
            $data['dz_time'] = strtotime($data['dz_time']);
        }
        if($data['contract_sn']){
            $contract_sn = $data['contract_sn'];
        }else{
            $contract_sn = "";
        }
        $subscription_area = '';
        if(isset($data['subscription_area'])){
            $subscription_area = $data['subscription_area'];
        }


        $data = array(
            'archives_sn' => $data['archives_sn'],
            'contract_sn' => $contract_sn,
            'shoppingguide' => $data['shoppingguide'],
            'closing_time' => strtotime($data['closing_time']),
            'closing_price' => $data['closing_price'],
            'free_price' => $data['free_price'],
            'subscription_area' => $subscription_area,
            'discount_ratio' => 0,
            'agent_user' => $data['agent_user'],
            'add_time' => !empty($data['closing_time'])?strtotime($data['closing_time']):time(),
            'status' => 2,
            'tomb_num' => $data['tomb_num'],
            'tomb_type' => $data['tomb_type'],
            'selling_price' => $data['selling_price'],
            'sales_total' => $data['selling_price'],
            'dz_time' => $data['dz_time'],
            'dz_create_time' => $data['dz_time'],
            'customer_id' => $data['customer_id'],
            'tomb_product_id'=>$data['tomb_product_id']
        );
        $addt = date("Y-m-d H:i:s",  $data['dz_time']);
        $data['bxq_end_date'] = strtotime($addt."+3year");
        $flag = false;
        if(isset($_POST['deceased_name'])){
            for ($i = 0; $i < count($_POST['deceased_name']); $i++) {

                if(!empty($_POST['deceased_name'][$i])){
                    if(empty($_POST['date_death'][$i])){
                        $this->error("逝世日期不能为空！");
                    }
                    if(empty($_POST['whq_start_date'])){
                        $this->error("维护期起始日不能为空！");
                    }
                    if(empty($_POST['type'][$i])){
                        $this->error("类型不能为空！");
                    }
                }
                if(!$flag || $_POST['type'][$i] != 1){
                    if($_POST['type'][$i] == 1){
                        $flag = true;
                        if(!empty($_POST['funeral_time'][$i])){
                            $_POST['whq_start_date'] = $_POST['funeral_time'][$i];
                        }
                    }
                }else{
                    $this->error("首位安葬逝者只能有一位！");
                }
            }
            $data['whq_start_date'] = strtotime($_POST['whq_start_date']);
            $data['whq_end_date'] = strtotime($_POST['whq_start_date']."+20year");
        }

        if(empty($data['customer_id'])){
            $data['customer_id'] = $this->file_buyer_add($dz_time,$data['tomb_num']);
        }else{

            $album = array(
//            客户名称
                'buyer_name' => $_POST['buyer_name'],
//            联系电话
                'buyer_phone' => $_POST['buyer_phone'],
//            客户账户
                'customer_account' => $_POST['customer_account'],
//            身份证号
                'cart_ID' => $_POST['cart_ID'],
                //            联系地址
                'customer_address' => $_POST['customer_address'],
//            证件类型
                'cart_type' => $_POST['cart_type'],
//            身份证地址
                'cart_upload' => $_POST['cart_upload'],
//            性别
                'buyer_sex' => $this->getCardSex($_POST['cart_ID'])
            );
            if($dz_time){
                $album['mz_dy_time'] = strtotime($dz_time);
                $album['is_dy_mz'] = 1;
                $album['tomb_number'] = $data['tomb_num'];
            }
            CustomerInfoModel::update($album,["id"=>$data['customer_id']]);
        }


        if(empty($order_id)){
            $data['tomb_id'] = $id;
            $order_id = Db::name('order')->insertGetId($data);
        }else{
            Db::name('order')
                ->where('order_id', $order_id)
                ->update($data);
        }

        // 插入合同表
        if(!empty($contract_enclosure)){
            // 判断有没有合同id
            $contracts = array(
                'contract_enclosure' => $contract_enclosure,
                'contract_enclosure_file_id' => $contract_enclosure_file_id,
            );
            if(!empty($sign_time)){
                $sign_time = strtotime($sign_time);
                $contracts['sign_time'] = $sign_time;
            }

            if(!empty($contract_id)){
                Db::name('contract')->where('order_id', $order_id)->update($contracts);
            }else{
                $contract_sn = ContractModel::get_contract_sn();
                $contracts['tomb_type'] = $data['tomb_type'];
                $contracts['tomb_id'] = $id;
                $contracts['contract_sn'] =$contract_sn;
                $contracts[ 'add_time'] = time();
                $contracts['order_id'] = $order_id;
                $contracts['customer_id'] =$data['customer_id'];
                Db::name('contract')->insertGetId($contracts);
            }
        }
        if(isset($_POST['deceased_name'])){
            if(count($_POST['deceased_name']) > 0){
                $this->deceasedEditPost($order_id,$data['customer_id']);
            }
            // 判断是购买未安葬还是全部安葬还是部分安葬
            //逝者姓名个数为0，则是购买未使用；逝者姓名个数少于墓穴数，则是部分安葬；逝者姓名个数等于墓穴数，则是全部安葬
            if(count(array_filter($_POST['deceased_name'])) == 0){
                $col['col_status'] = "5";
            }elseif(count(array_filter($_POST['deceased_name']))<$_POST['tomb_regulation']){
                $col['col_status'] = "6";
            }else{
                $col['col_status'] = "7";
            }
//            Db::name("tombarea_row_col")->where(array('colID' => $id))->update($col);
        }else{
        }

        $col['col_status'] =$col_status;
        TombareaRowColModel::update($col,array('colID' => $id));
        $this->success("保存成功", url('Archives/visua'));
    }

    /*购买人信息--编辑保存*/
    public function buyerEditPost($order_id)
    {

        $order_info = Db::name('order')->where(['order_id' => $order_id])->find();

        $data = array(
            'buyer_name' => $_POST['buyer_name'],
            'buyer_phone' => $_POST['buyer_phone'],
            'customer_account' => $_POST['customer_account'],
            'cart_type' => $_POST['cart_type'],
            'cart_ID' => $_POST['cart_ID'],
            'customer_address' => $_POST['customer_address'],
            'add_time' => time(),
            'add_user' => cmf_get_current_admin_id(),
        );
        if($order_info['customer_id']){
            $customer_id = $order_info['customer_id'];
            Db::name('customer_info')
                ->where('id', $order_info['customer_id'])
                ->update($data);
        }else{
            $customer_id = Db::name("customer_info")->insert($data,false,true);
        }

        return $customer_id;
    }

    /*逝者信息--编辑保存*/
    public function deceasedEditPost($order_id,$customer_id)
    {
        $album = array(
            'add_time' => time(),
            'add_user' => cmf_get_current_admin_id(),
        );
        //取name值
        $id = $this->request->param("deceased_id");
        $deceased_name = $_POST['deceased_name'];
        $gender = $_POST['gender'];
        $convergence_type = $_POST['convergence_type'];
        $type = $_POST['type'];
        $receptionist = $_POST['receptionist'];
        $relationship_users = $_POST['relationship_users'];
        $cremation_certificate = $_POST['cremation_certificate'];
        $cremation_certificate_file_id = $_POST['cremation_certificate_file_id'];
        $tomb_num = $_POST['tomb_num'];
        $tomb_id = $_POST['tomb_id'];

        //$_POST请求数据库字段
        $date_birth = $_POST['date_birth'];
        $date_death = $_POST['date_death'];
        //$date_funeral = $_POST['date_funeral'];
        $funeral_time = $_POST['funeral_time'];

        for ($i = 0; $i < count($deceased_name); $i++) {
            if(!empty($deceased_name[$i])){
                $album['order_id'] = $order_id;
                $album['customer_id'] = $customer_id;
                $album['tomb_num'] = $tomb_num;
                $album['tomb_id'] = $tomb_id;
                $album['deceased_name'] = $deceased_name[$i];
                $album['gender'] = $gender[$i];
                $album['convergence_type'] = $convergence_type[$i];
                $album['gender'] = $gender[$i];
                $album['type'] = $type[$i];
                $album['receptionist'] = $receptionist[$i];
                $album['relationship_users'] = $relationship_users[$i];
                $album['cremation_certificate'] = $cremation_certificate[$i];
                $album['cremation_certificate_file_id'] = $cremation_certificate_file_id[$i];
                //时间类型，先获取值再转
                $album['date_birth'] = $date_birth[$i];
                $album['date_death'] = $date_death[$i];
                //$album['date_funeral'] = strtotime($date_funeral[$i]);
                $album['funeral_time'] = $funeral_time[$i];
                if($id[$i]){
                    Db::name("Archives_deceased")->where(array('deceased_id' => $id[$i]))->update($album);
                }else{
                    Db::name("Archives_deceased")->insert($album);
                }
            }
        }
    }

    /*档案删除*/
    public function delete()
    {
        $order_id = $this->request->param("order_id");
        $tomb_id = $this->request->param("tomb_id");

        if ($order_id != '' || $order_id != null) {
            Db::name("Archives_deceased")->where('order_id', $order_id)->delete();
            Db::name("order")->where('order_id', $order_id)->delete();

            Db::name('tombarea_row_col')->where(['colID' => $tomb_id])->update(['col_status' => 1]);
            $this->success("删除成功", url('Archives/archives'));
        } else {
            $this->error("数据出错", url('Archives/archives'));
        }

    }

}